!function(angular){
    const m = angular.module('ngLightPagination', []);
    m.config(function ($compileProvider) {
        $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|itms\-apps|ftp|mailto|file|javascript):/);
    });
    m.component('ngLightPagination',{
        template:function ($element, $attrs) {
            const cc = `
            <li  class="paginate_button page-item previous"
             ng-repeat="item in els"  ng-class="{active:item.active}">
                <a href="{{ item.goto }}" class="page-link">
                    {{ item.page }}
                </a>
            </li>
            `;
            return `
                
                <ul class="pagination">
                    <li class="paginate_button page-item previous">
                        <a href="{{hasPrev?('?page='+(current-1)):'javascript:;'}}" class="page-link">
                        
                            上一页
                        </a>
                    </li>
                    ${cc}
                    <li class="paginate_button page-item previous">
                        <a href="{{hasNext?('?page='+(current+1)):'javascript:;'}}" class="page-link">
                            下一页
                        </a>
                    </li>
                </ul>
                
            `
        },
        controller: function ($scope,$attrs) {
            const {current,total,size} = $attrs;
            const pageBufferSize = 2;
            const currentNum = parseInt(current);
            const pageLengthReal = total / size;
            const pageLength = Math.ceil(pageLengthReal);
            const pageArray = [];
            if (pageLength<=5 + pageBufferSize*2) {
                for (let i=1;i<=pageLength;i++){
                    const active = currentNum === i;
                    pageArray.push({
                        active:active,
                        page:`${i}`,
                        goto:`?page=${i}`,
                    });
                }
            }else{
                const prevItemTitle = '...';
                const nextItemTitle = '...';

                const jumpPrevEx= {
                    active:false,
                    page:prevItemTitle,
                    goto:function () {
                        return `?page=${Math.max(1, currentNum - 5)}`;
                    }()
                };
                const jumpNextEx= {
                    active:false,
                    page:nextItemTitle,
                    goto: function () {
                        return `?page=${Math.min(pageLength, currentNum + 5)}`;
                    }()
                };
                const lastPager = {
                    active:false,
                    page:`${pageLength}`,
                    goto:`?page=${pageLength}`
                };
                const firstPager = {
                    active:false,
                    page:`1`,
                    goto:`?page=1`
                };
                let left = Math.max(1, currentNum - pageBufferSize);
                let right = Math.min(currentNum + pageBufferSize, pageLength);
                if (currentNum - 1 <= pageBufferSize) {
                    right = 1 + pageBufferSize * 2;
                }
                if (pageLength - currentNum <= pageBufferSize) {
                    left = pageLength - pageBufferSize * 2;
                }
                for (let i = left; i <= right; i++) {
                    const active = currentNum === i;
                    pageArray.push({
                        active:active,
                        page:`${i}`,
                        goto:`?page=${i}`
                    });
                }
                if (currentNum - 1 >= pageBufferSize * 2 && currentNum !== 1 + 2) {
                    pageArray.unshift(jumpPrevEx);
                }
                if (pageLength - currentNum >= pageBufferSize * 2 && currentNum !== pageLength - 2) {
                    pageArray.push(jumpNextEx);
                }
                if (left !== 1) {
                    pageArray.unshift(firstPager);
                }
                if (right !== pageLength) {
                    pageArray.push(lastPager);
                }
            }
            $scope.els=pageArray;
            $scope.current = currentNum;
            $scope.hasPrev = function () {
                return currentNum>1;
            }();
            $scope.hasNext = function () {
                return currentNum<pageLength;
            }();
        }
    });
}(angular);